﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace YNTK.Common.Helper
{
    /// <summary>
    /// RSA加密解密
    /// </summary>
    public class RSAHelper
    {
        /// <summary>
        /// 生成秘钥
        /// </summary>
        /// <param name="privarteKey">私钥秘钥</param>
        /// <param name="publicKey">公钥密钥</param>
        /// <param name="KeySize">秘钥大小</param>
        public static void Generator(out string privarteKey, out string publicKey, int KeySize = 1024)
        {
            RSACryptoServiceProvider rea = new RSACryptoServiceProvider(KeySize);
            privarteKey = rea.ToXmlString(true);
            publicKey = rea.ToXmlString(false);
        }


        /// <summary> 
        /// RSA加密数据 
        /// </summary> 
        /// <param name="content">要加密数据</param> 
        /// <param name="publickey">公钥密钥</param> 
        /// <returns></returns> 
        public static string RSAEncryption(string content, string publickey)
        {

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.FromXmlString(publickey);
            cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
            return Convert.ToBase64String(cipherbytes);

        }

        /// <summary> 
        /// RSA解密数据 
        /// </summary> 
        /// <param name="content">要解密数据</param> 
        /// <param name="privatekey">私钥秘钥</param> 
        /// <returns></returns> 
        public static string RSADecrypt(string content, string privatekey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.FromXmlString(privatekey);
            cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);

            return Encoding.UTF8.GetString(cipherbytes);

        }

        /// <summary>
        /// 生成数字签名
        /// </summary>
        /// <param name="originalText">原文</param>
        /// <param name="privateKey">私钥秘钥</param>
        /// <returns></returns>
        public static string GenSign(string originalText, string privateKey)
        {
            byte[] byteData = Encoding.UTF8.GetBytes(originalText);
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(privateKey);
            //使用SHA1进行摘要算法，生成签名
            byteData = provider.SignData(byteData, new SHA1CryptoServiceProvider());
            return Convert.ToBase64String(byteData);
        }

        /// <summary>
        /// 验证签名
        /// </summary>
        /// <param name="originalText">原文</param>
        /// <param name="signedData">签名</param>
        /// <param name="publicKey">公钥密钥</param>
        /// <returns></returns>
        public static bool VerifySigned(string originalText, string signedData, string publicKey)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            provider.FromXmlString(publicKey);
            byte[] byteData = Encoding.UTF8.GetBytes(originalText);
            byte[] signData = Convert.FromBase64String(signedData);
            return provider.VerifyData(byteData, new SHA1CryptoServiceProvider(), signData);
        }


    }
}
